<template>
    <div class="wrapper">
        <div style="margin: 200px auto; background-color: #fff; width: 350px; height: 350px; padding: 20px; border-radius: 10px">
            <div style="margin: 20px 0; text-align: center; font-size: 24px"><b>登 录</b></div>
            <el-form :model="user" :rules="rules" ref="userForm" >

                <el-form-item prop="username">
                    <el-input size="default" style="margin: 10px 0"  v-model="user.username"
                              placeholder="请输入用户名"

                    ></el-input>
                </el-form-item>

                <el-form-item prop="password">
                    <el-input  placeholder="请输入密码" size="default"  style="margin: 10px 0"  show-password v-model="user.password"></el-input>
                </el-form-item>


                <el-form-item prop="validCode">
                    <div style="display: flex">

                        <el-input  style="width:220px" placeholder="请输入验证码&nbsp;&nbsp;(不区分大小写)"   v-model="user.validCode"></el-input>
                        <ValidCode @input="createValidCode"></ValidCode>
                    </div>
                </el-form-item>




                <el-form-item style="float: right">
                    <el-button type="primary" size="small"  autocomplete="off" @click="login">登录</el-button>
                    <el-button type="warning" size="small"  autocomplete="off" @click="$router.push('/register')">注册</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import axios from 'axios'
    import request from '../utils/request'
    import { ElMessage } from 'element-plus'
    import ValidCode  from '../components/ValidCode'
    export default {
        name: "index",
        components: {
            ValidCode
        },
        data() {
            return {
                user: {},
                validCode: '',
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 2, max: 10, message: '长度在 2 到 5 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
                    ],
                    validCode: [
                        { required: true, message: '请输入验证码', trigger: 'blur' },
                        { min: 4, max: 4, message: '长度为 4 字符', trigger: 'blur' }
                    ],
                }
            }
        },
        methods: {
            createValidCode(data){
                this.validCode =  data
            },
            login() {
                this.$refs['userForm'].validate((valid) => {
                    if (valid) {  // element提供的表单校验合法
                        if (!this.user.validCode) {
                            ElMessage({
                                showClose: true,
                                message: '请填写验证码',
                                type: 'error',
                            })
                            return
                        }
                        if (this.user.validCode.toLowerCase() !== this.validCode.toLowerCase() ) {
                            ElMessage({
                                showClose: true,
                                message: '验证码错误',
                                type: 'error',
                            })
                            return
                        }






                        
                        request.post('/user/login',this.user)
                            .then((res) => {
                                // console.log(res)
                                if (res.code === 200) {

                                    // console.log(res)

                                    ElMessage({
                                        showClose: true,
                                        message: '登录成功',
                                        type: 'success',
                                    })
                                    localStorage.setItem("user",JSON.stringify(res.data))
                                    sessionStorage.setItem("user",JSON.stringify(res.data))
                                    this.user = []
                                    setTimeout(()=>{
                                        this.$router.push("/")
                                    },1000)
                                }else {
                                    ElMessage({
                                        showClose: true,
                                        message: res.message,
                                        type: 'error',
                                    })
                                }
                            })
                    } else {
                        return false;
                    }
                });
            },


        }
    }
</script>

<style>
    .wrapper {
        height: 100vh;
        background-image: linear-gradient(to bottom right, #FC466B , #3F5EFB);
        overflow: hidden;
    }
</style>
